﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CodeGenerator.Core;
using CodeGenerator.Common;
using System.Text.RegularExpressions;

namespace CodeGenerator.UI.BuildHControl
{
    public partial class UcFormControl : UserControl
    {
        public UcFormControl()
        {
            InitializeComponent();
        }

        private void UcListControl_Load(object sender, EventArgs e)
        {
            ServerInfo server = Constant.CurrentServerInfo;
            if (server != null && !string.IsNullOrEmpty(Constant.DatabaseName))
            {
                IDatabase database = DBFactory.Database(server.DBType);

                string dbName = Constant.DatabaseName;
                string tableName = Constant.TableName;
                List<ColumnInfo> columnInfoList = database.GetColumnInfoList(server, dbName, tableName);

                List<CreateFormCode> list = new List<CreateFormCode>();

                foreach (var column in columnInfoList)
                {
                    CreateFormCode model = new CreateFormCode();
                    model.No = column.Id;
                    model.Lable = column.Comments;
                    model.Field = column.Name;
                    model.UIControlType = column.UIControlType;
                    model.UIControlValidator = "==请选择==";
                    model.MaxLength = column.MaxLength;
                    model.ControlColspan = 12;
                    model.SortCode = Convert.ToInt32(column.Id);
                    model.Show = true;

                    list.Add(model);
                }

                dataGridView1.DataSource = list;
                DataGridViewEx.AutoSizeColumn(dataGridView1);
            }
        }

        public List<CreateFormCode> GetFormCode()
        {
            List<CreateFormCode> list = new List<CreateFormCode>();

            List<CreateFormCode> data = (List<CreateFormCode>)dataGridView1.DataSource;
            foreach (var formCode in data)
            {
                CreateFormCode model = new CreateFormCode();
                model.No = formCode.No;
                model.Lable = formCode.Lable;
                model.Field = formCode.Field;
                model.UIControlType = formCode.UIControlType;
                model.UIControlStyle = "form-control";
                if(formCode.UIControlType==ControlType.DateTime.ToString())
                {
                    model.UIControlStyle = "form-control input-wdatepicker";
                }
                else if (model.UIControlType == ControlType.Select.ToString())
                {
                    model.UIControlStyle = "ui-select";
                }
                model.UIControlValidator = "";
                if(formCode.UIControlValidator!="==请选择==")
                {
                    string[] arry = Regex.Split(formCode.UIControlValidator, " - ", RegexOptions.IgnoreCase);
                    model.UIControlValidator = arry[0];
                }
                model.MaxLength = formCode.MaxLength;
                model.ControlColspan = formCode.ControlColspan;
                model.SortCode = formCode.SortCode;
                model.Show = formCode.Show;
                list.Add(model);
            }
            list = list.OrderBy(t => t.SortCode).ToList(); 

            return list;
        }
    }
}
